package com.sakura.ucenter.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sakura.commonutils.R;
import com.sakura.ucenter.entity.UcenterMember;
import com.sakura.ucenter.entity.vo.RegisterVo;
import com.sakura.ucenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "UcenterMemberController",description = "会员表 前端控制器")
@Slf4j
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation("会员登录")
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        log.info("the token is : "+token);
        return R.ok().data("token",token);
    }


    @ApiOperation("会员注册")
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation("根据会员id获取会员信息")
    @GetMapping("info/{memberId}")
    public R userInfo(@PathVariable String memberId){
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("member",member);
    }

    @ApiOperation("根据token值获取会员信息")
    @GetMapping("infoByToken/{token}")
    public R getUserInfoByToken(@PathVariable String token){
        JSONObject info = memberService.getUserInfoByToken(token);
        return R.ok().data("info",info);
    }


    @ApiOperation("统计第day天的会员注册量")
    @GetMapping("countRegister/{day}")
    public R registerCount(@PathVariable String day){
        Integer count = memberService.countRegisterByDay(day);
        return R.ok().data("countRegister",count);
    }

}
